package com.canbot.u05.activity.presidemode.newcode;

import android.content.Intent;
import android.util.Log;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.PicturnEmotionBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.activity.presidemode.newcode.view.ItemChoiceView;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;

import butterknife.BindView;

public class NPictureChoiceSettingActivity extends BaseHostActivity {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.item_choice_picture)
    ItemChoiceView itemChoicePicture;
    @BindView(R.id.item_choice_emotion)
    ItemChoiceView itemChoiceEmotion;
    @BindView(R.id.item_choice_none_picture)
    ItemChoiceView itemChoiceNone;
    private PicturnEmotionBean picturnEmotionBean;
    private int resultCode;

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.VoiceAndEyesInfo voiceTip = new MapVoiceAndEyes.VoiceAndEyesInfo(MapVoiceAndEyes.PRESENTER_VOICE_DIR + "jbkz_tpxz_u05_001.wav", "26");
        MapVoiceAndEyes.playMapVoiceAndEyes(voiceTip);
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    finishResult();
                    break;
            }
        });
        itemChoicePicture.setOnClickListener(v -> {
            Intent intent = new Intent(this, NChoicePictureOrEmotionActivity.class);
            intent.putExtra(NChoicePictureOrEmotionActivity.ROUTE, NModuleType.PICTURE);
            gotoActivityForResult(intent, NModuleType.PICTURE);
        });
        itemChoiceEmotion.setOnClickListener(v -> {
            Intent intent = new Intent(this, NChoicePictureOrEmotionActivity.class);
            intent.putExtra(NChoicePictureOrEmotionActivity.ROUTE, NModuleType.EMOTION);
            gotoActivityForResult(intent, NModuleType.EMOTION);
        });
        itemChoiceNone.setOnClickListener(v -> {
            itemChoicePicture.cancle();
            itemChoiceEmotion.cancle();
            itemChoiceNone.select();
            resultCode = NModuleType.PICTURE_NONE;
            if (picturnEmotionBean == null) {
                picturnEmotionBean = new PicturnEmotionBean();
            }
            picturnEmotionBean.setType(NModuleType.PICTURE_NONE);
            picturnEmotionBean.setFileName(getString(R.string.choice_none_picture));
        });
        initIntent();

    }

    private void finishResult() {
        if (picturnEmotionBean != null && picturnEmotionBean.isSelect()) {
            Intent intent = new Intent();
            intent.putExtra(DATA, picturnEmotionBean);
            Log.e(TAG, "initEventData: " + resultCode);
            setResult(resultCode, intent);
            finish();
        } else {
            // Toast.makeText(this, getString(R.string.picturn_emotion_is_null), Toast.LENGTH_SHORT).show();
            picturnEmotionBean = new PicturnEmotionBean();
            picturnEmotionBean.setType(NModuleType.PICTURE_NONE);
            picturnEmotionBean.setFileName(getString(R.string.choice_none_picture));
            Intent intent = new Intent();
            intent.putExtra(DATA, picturnEmotionBean);
            setResult(NModuleType.PICTURE_NONE, intent);
            finish();
        }
    }

    private static final String TAG = "NPictureChoiceSettingAc";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        this.resultCode = resultCode;
        if (intent != null) {
            picturnEmotionBean = (PicturnEmotionBean) intent.getSerializableExtra(DATA);
            if (picturnEmotionBean != null) {
                int type = picturnEmotionBean.getType();
                if (type == NModuleType.PICTURE) {
                    itemChoicePicture.select();
                } else {
                    itemChoiceEmotion.select();
                }
                finishResult();
            }
        }
    }

    private void initIntent() {
        picturnEmotionBean = (PicturnEmotionBean) getIntent().getSerializableExtra(DATA);
        if (picturnEmotionBean != null) {
            int type = picturnEmotionBean.getType();
            if (type == NModuleType.PICTURE) {
                itemChoicePicture.select();
            } else if (type == NModuleType.EMOTION){
                itemChoiceEmotion.select();
            }else {
                itemChoiceNone.select();
            }
        } else {
            itemChoiceNone.select();
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_npicture_choice_settings;
    }

}
